

class HistoryKeyword {
    static MAX_ITEM_COUNT = 3
    static KEY = 'keywords'
    keywords=[]

    constructor(){
        this.keywords = this._getLocalKeywords()
    }

    save(keywords){
        const items = this.keywords.filter(k=>{
            return k===keywords
        })
        if(items.length !== 0){
            return
        }
        if(this.keywords.length >= HistoryKeyword.MAX_ITEM_COUNT){
            this.keywords.pop()
        }
        this.keywords.unshift(keywords)
        this._reflashLocal()
    }

    get(){
        return this.keywords
    }

    clear(){
        this.keywords=[]
        this._reflashLocal()
    }

    _reflashLocal(){
        wx.setStorageSync(HistoryKeyword.KEY,this.keywords)
    }

    _getLocalKeywords(){
        const keywords=wx.getStorageSync(HistoryKeyword.KEY)
        if(!keywords){
            wx.setStorageSync(HistoryKeyword.KEY,[])
            return []
        }
        return keywords
    }
}

export {
    HistoryKeyword
}